<?php

require_once BASE_PATH . 'application/models/itemdao' . EXT;

class galleryDAO extends itemDAO {

    function __construct() {

        // Call the Model constructor
        parent::__construct();
    }

    function _defaultSQL() {
        parent::_defaultSQL(0);
        $this->db->where(array('gallery !=' => ''));
    }

    function getImagesById($id) {

        $this->db->where_in('id_item', $id);
        $query = $this->db->get('k2_galeries');
        return $result = $query->result();
    }

    function getImagesByCategoryId($id,$data=array()) {
        $results = $this->findByCategory($id,$data);
        $records = null;
        if (!empty($results)) {
            foreach ($results as $record) {
                $gallery = $this->getImagesById($record->id);
                $records[] = array_merge((array) $record, array("gallery" => $gallery));
            }
            $results = (Object) $records;
        }
        else
        {
            $results = null;
        }
        return $results;
    }
    
    function getImagesByCategoriesId($id,$data=array()) {
        $results = $this->findItemsByCategoriesId($id,$data);
        $records = null;
        if (!empty($results)) {
            foreach ($results as $record) {
                $gallery = $this->getImagesById($record->id);
                $records[] = array_merge((array) $record, array("gallery" => $gallery));
            }
            $results = (Object) $records;
        }
        else
        {
            $results = null;
        }
        return $results;
    }
	
	    function test($id,$data=array()) {
        $this->load->model('categoryDAO');
		//$result[]=null;
		foreach($data->categorias as $items){
			//$master = $this->getMainCategoryById($items->catid);
			if($items->catid==$id){
				$result[]=$items;
			}else{
				if(($items->catid!=1)&&($items->catid!=2)&&($items->catid!=3)&&($items->catid!=4)&&($items->catid!=5)&&($items->catid!=6)&&($items->catid!=41)){
				if(($this->categoryDAO->getMainCategoryById($items->catid))==($id))
				$result[]=$items;
			
			 }//if
			
			}//else
		
		}//foreach
		return $result;
    }
	
    function getCategories()
	{
		$this->load->model("categorydao");
		
		$this->_defaultSQL();
		$this->db->select("category,catid");
		$this->db->group_by("catid");
		$query = $this->db->get(parent::$this->table);
		$categorias = $query->result();
	
		$idsCat = "";
		$idsCats = "";
		$totalCats = "";
		
		foreach($categorias as $categoria){
			$idsCat .= $categoria->catid;
			$idsCats .= $categoria->catid;
			
			$totalCats .=count($this->findByCategory($categoria->catid));
			$totalCats.="_";
			
			$idsCats .="_";
			$idsCat .= ",";
			
		}
		
		$idsCat=substr($idsCat, 0,-1);
		$idsCats=substr($idsCats, 0,-1);
		$totalCats=substr($totalCats, 0,-1);
		
		$result =array(
			       "categorias"=>$this->findByCategory($idsCat,array("order"=>array("catid","Asc"),"select"=>"id,catid,title,gallery,category")),
			       "ids"=>$idsCats,
			       "totalCats"=>$totalCats
			       );
		$result = (Object)$result;
		
		foreach($result->categorias as $galerias){
			$galerias->gallery = $this->getImagesById($galerias->id);
		}
		
		return $result;
	}

}

?>